<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\BaseController;
use app\common\lib\Arr;
use app\common\lib\Show;
use think\facade\Log;
use think\facade\Request;
use app\common\business\Category as BusiCate;

class Category extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $busiCate = new BusiCate();
        $category = $busiCate->getNormalCate("id,pid,name");

        if (!$category) {
            return Show::success([], '数据为空');
        }
        $result = Arr::getClassTree($category);
        $result = Arr::sliceClassTree($result);

        return Show::success($result);
    }

    /**
     * 分类信息搜索
     *
     * @return array
     */
    public function search()
    {
        $categoryId = Request::param('id', 0, 'intval');

        $busiCate = new BusiCate();
        $category = $busiCate->searchCategoryByCategoryId($categoryId);

        return Show::success($category);
    }

    public function subcategory()
    {
        $pid = Request::param('id', 0, 'intval');
        $busiCate = new BusiCate();
        $result   = $busiCate->getNormalByPid($pid, 'id,name');
        return Show::success($result);
    }
}
